package com.txsj.scan.activity.allocationapply;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.txsj.scan.R;
import com.txsj.scan.activity.ReadLabelActivity;
import com.txsj.scan.adapter.AllocationApplyGoodsAdapter;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.bean.AllocationApplyDetailsBean;
import com.txsj.scan.bean.EventAllocationBean;
import com.txsj.scan.bean.GoodsInfo;
import com.txsj.scan.presenter.AllocationApplyDetailsPresenter;
import com.txsj.scan.view.IAllocationApplyDetailsView;
import com.txsj.scan.widget.popup.PopupAllocationExamine;
import com.txsj.scan.widget.popup.PopupReject;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class AllocationApplyDetailsActivity extends BaseActivity<AllocationApplyDetailsPresenter> implements IAllocationApplyDetailsView, View.OnClickListener {


    String billId;
    AllocationApplyGoodsAdapter adapter;
    RecyclerView recyclerView;
    LinearLayout lay_takeState;

    @Override
    protected AllocationApplyDetailsPresenter providePresenter() {
        return new AllocationApplyDetailsPresenter(this);
    }

    @Override
    public Context context() {
        return this;
    }

    TextView tv_form_state, tv_form_num, tv_form_goodsCategroy, tv_form_goodsCount, tv_form_takeName, tv_form_addTime, tv_form_remark;
    Button btn_reject, btn_adopt; //驳回  审核通过

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_allocation_apply_details);
        initView();
    }

    private void initView() {
        EventBus.getDefault().register(this);
        btn_reject = (Button) findViewById(R.id.btn_reject);
        btn_reject.setOnClickListener(this);
        btn_adopt = (Button) findViewById(R.id.btn_adopt);
        btn_adopt.setOnClickListener(this);
        lay_takeState = (LinearLayout) findViewById(R.id.lay_takeState);
        recyclerView = findViewById(R.id.recyclerView);
        tv_form_state = findViewById(R.id.tv_form_state);
        tv_form_num = findViewById(R.id.tv_form_num);
        tv_form_goodsCategroy = findViewById(R.id.tv_form_goodsCategroy);
        tv_form_goodsCount = findViewById(R.id.tv_form_goodsCount);
        tv_form_takeName = findViewById(R.id.tv_form_takeName);
        tv_form_addTime = findViewById(R.id.tv_form_addTime);
        tv_form_remark = findViewById(R.id.tv_form_remark);
        adapter = new AllocationApplyGoodsAdapter(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        billId = getIntent().getStringExtra("bill_id");
        if (!TextUtils.isEmpty(billId)) {
            presenter.getFormDetails(billId + "");
        } else {
            showErrorMsg("数据错误 请返回刷新重试");
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_reject:
                new PopupReject(this, btn_reject)
                        .builder()
                        .setSave(new PopupReject.IOnSaveClick() {
                            @Override
                            public void onSaveClick(String remark) {
                                if (TextUtils.isEmpty(remark)) {
                                    presenter.setAllocationFromStates("-4", "");
                                } else {
                                    presenter.setAllocationFromStates("-4", remark);
                                }
                            }
                        }).show();
                break;
            case R.id.btn_adopt:
                new PopupAllocationExamine(this, btn_adopt)
                        .builder()
                        .setFirseClick(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(AllocationApplyDetailsActivity.this, AllocationBatchActivity.class);
                                intent.putExtra("detailsBean", new Gson().toJson(mDetailsBean));
                                startActivity(intent);
                            }
                        })
                        .setSecondClick(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                List<GoodsInfo> goodsList = new ArrayList<>();
                                for (int i = 0; i < mDetailsBean.getDetails().size(); i++) {
                                    GoodsInfo mGoods = new GoodsInfo();
                                    mGoods.setBatch_id(mDetailsBean.getDetails().get(i).getBatch_id());
                                    mGoods.setGoods_id(mDetailsBean.getDetails().get(i).getGoods_id());
                                    mGoods.setGoods_name(mDetailsBean.getDetails().get(i).getGoods_name());
                                    mGoods.setAdd_time(mDetailsBean.getDetails().get(i).getAdd_time());
                                    mGoods.setCount(mDetailsBean.getDetails().get(i).getGoods_count());
                                    mGoods.setRepertory(mDetailsBean.getDetails().get(i).getPast_inventory());
                                    goodsList.add(mGoods);
                                }

                                Intent intent = new Intent(AllocationApplyDetailsActivity.this, ReadLabelActivity.class);
                                intent.putExtra("data", new Gson().toJson(goodsList));
                                intent.putExtra("detailsBean", new Gson().toJson(mDetailsBean));
                                intent.putExtra("from", "AllocationApplyDetailsActivity");
                                intent.putExtra("type", "0");
                                startActivity(intent);
                            }
                        })
                        .show();
                break;

        }
    }

    AllocationApplyDetailsBean mDetailsBean;

    @Override
    public void loadSuccess(AllocationApplyDetailsBean allocationBean) {
        if (null == allocationBean) return;
        mDetailsBean = allocationBean;
        tv_form_state.setText(allocationBean.getBill_status_name());
        tv_form_num.setText(allocationBean.getBill_number());
        tv_form_goodsCategroy.setText(allocationBean.getGoods_category_count() + "");
        tv_form_goodsCount.setText(allocationBean.getGoods_count() + "");
        tv_form_takeName.setText(allocationBean.getCheck_user_name());
        tv_form_addTime.setText(allocationBean.getAdd_time());
        if (!TextUtils.isEmpty(allocationBean.getBill_remark()))
            tv_form_remark.setText(allocationBean.getBill_remark());
        else {
            tv_form_remark.setText("暂无");
        }

        //  -1待审核 -2待出库 -3待入库 -4驳回 -5已完成 -6已作废
        if (allocationBean.getBill_status_name().equals("待审核")) {
            lay_takeState.setVisibility(View.VISIBLE);
        } else {
            lay_takeState.setVisibility(View.GONE);
        }

        if (allocationBean.getDetails() != null && allocationBean.getDetails().size() > 0) {
            adapter.setGoodsList(allocationBean.getDetails());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void loadFailed(String errMsg) {
        showErrorMsg(errMsg);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(EventAllocationBean message) {
        if (message.isRefresh()) {
            this.finish();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
